package com.example.keli.base.utils;

import android.text.TextUtils;

/**
 * Created by LiuLei on 2016/11/29.
 *
 */

public class StringUtils {

    //151 1111 1111 -> 151 **** 1111
    public static String dealPhoneNumber(String phoneNumber) {
        if (!TextUtils.isEmpty(phoneNumber)) {
            int len = phoneNumber.length();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < len; i++) {
                if (i > 2 && i < 7) {
                    builder.append("*");
                } else {
                    builder.append(phoneNumber.charAt(i));
                }

                if (i == 2 || i == 6) {
                    if (i != len - 1)
                        builder.append(" ");
                }
            }

            return builder.toString();
        }
        return "";
    }

    //张国荣->->->张**
    public static String dealName(String name) {
        if (!TextUtils.isEmpty(name)) {
            int len = name.length();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < len; i++) {
                if (i >= 1) {
                    builder.append("*");
                } else {
                    builder.append(name.charAt(i));
                }
            }

            return builder.toString();
        }
        return "";
    }

    public static byte[] reverseBytes(byte[] a) {
        int len = a.length;
        byte[] b = new byte[len];
        for (int k = 0; k < len; k++) {
            b[k] = a[a.length - 1 - k];
        }
        return b;
    }

    // byte转十六进制字符串
    public static String bytes2HexString(byte[] bytes) {
        String ret = "";
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }

}
